const db = uniCloud.database()
const dbCmd = db.command;
module.exports = {
	main: async (event) => {
		let {
			res,
			data,
			action
		} = event;
		switch (action) {
			case 'query':
				res.data = await getPage(data);
				break;
			case 'queryOne':
				res.data = await getPageOne(data);
				break;
			case 'queryById':
				res.data = await getPageById(data);
				break;
			case 'add':
				res = await addPage(data);
				break;
			case 'edit':
				res = await editPage(data);
				break;
			case 'del':
				res = await delPage(data);
				break;
			default:
				res = {
					code: 402,
					msg: '非法访问'
				}
				break;
		}
		return res;
	}
}

/**
 * 获取页面列表
 */
async function getPage(data) {
	var result = await db.collection('page').where({
			'comType': 'index',
			'pageTitle': eval('/' + data.name + '/')
		})
		.skip((data.page-1)*data.limit)
		.limit(data.limit)
		.orderBy('updatetime', 'desc')
		.get()
	var resultTotal = await db.collection('page').where({
			'comType': 'index',
			'pageTitle': eval('/' + data.name + '/')
		})
		.count()
	let res = {
		total:resultTotal.total,
		records:result.data
	}
	return res
}
/**
 * 获取页面列表
 */
async function getPageOne(data) {
	var result = await db.collection('page')
		.orderBy('updatetime', 'desc')
		.get()
	return result.data
}
/**
 * 获取页面数据通过ID
 */
async function getPageById(data) {
	var result = await db.collection('page').doc(data.id)
		.get()
	return result.data
}
/**
 * 添加页面
 */
async function addPage(data) {
	var res = {}
	var tmp = await db.collection('page').where({
		'pageTitle': data.pageTitle
	}).get()
	if (tmp.data[0]) {
		res.code = 402;
		res.msg = '页面标题已存在，请重新输入'
	} else {
		const result = await db.collection('page').add({
			diyData: data.diyData,
			templateName: data.templateName,
			pageTitle: data.pageTitle,
			comType: data.comType,
			pageShareDetail: data.pageShareDetail,
			backgroundColor: data.backgroundColor,
			template: data.template,
			updatetime: data.updatetime
		})
		res.code = 200;
		res.data = result.id
	}
	return res;
}
/**
 * 更新页面
 */
async function editPage(data) {
	var res = {}
	var t1 = await db.collection('page').where({
		'_id': data.id
	}).get();
	let tmp = 0;
	if (t1.data[0].pageTitle == data.pageTitle) {
		tmp = 1;
	} else {
		var t2 = await db.collection('page').where({
			'pageTitle': data.pageTitle
		}).get()
		if (t2.data[0]) {
			res.code = 402;
			res.msg = '页面标题已存在，请重新输入'
			tmp = 2
		} else {
			tmp = 1
		}
	}
	if (tmp == 1) {
		const result2 = await db.collection('page').doc(data.id).update({
			diyData: data.diyData,
			templateName: data.templateName,
			pageTitle: data.pageTitle,
			comType: data.comType,
			pageShareDetail: data.pageShareDetail,
			backgroundColor: data.backgroundColor,
			template: data.template,
			updatetime: data.updatetime
		})
		res.code = 200;
		res.data = result2.data
	}
	return res;
}
/**
 * @param {Object} data
 * 删除页面
 */
async function delPage(data) {
	var res = {}
	var tmp = await db.collection('page').where({
		'_id': data.id
	}).remove()
	if (tmp.deleted === 1) {
		res.code = 200
		res.data = '删除成功'
	} else {
		res.code = 402
		res.msg = '删除失败'
	}
	return res
}
